package com.zshan.clinic.admin.service.config;

import com.zshan.clinic.admin.constant.RedisKeyPrefix;
import com.zshan.clinic.admin.entity.vo.*;
import com.zshan.clinic.admin.mapper.config.SpecDictMapper;
import com.zshan.clinic.common.constant.CommonConstant;
import com.zshan.clinic.common.constant.DictConstant;
import com.zshan.clinic.common.enums.*;
import com.zshan.clinic.common.exception.BusinessFailException;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.common.util.page.PageUtils;
import com.zshan.clinic.common.util.pinyin.PinyinUtils;
import com.zshan.clinic.common.util.redis.RedisUtil;
import com.zshan.clinic.common.util.string.StringUtil;
import com.zshan.clinic.common.util.tree.TreeParser;
import com.zshan.clinic.database.dao.*;
import com.zshan.clinic.database.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 系统常量
 */
@Service
@Slf4j
public class ClinicDictService {

    /**
     * 系统常量
     */
    @Autowired
    private DictSysMapper dictSysMapper;


    @Autowired
    private DictSysItemMapper dictSysItemMapper;

    /**
     * 区域
     */
    @Autowired
    private DictRegionMapper dictRegionMapper;

    /**
     * 使用频率常量 一天一次
     */
    @Autowired
    private DictFrequencyMapper dictFrequencyMapper;

    /**
     * 药品用法常量 口服
     */
    @Autowired
    private DictUsageMapper dictUsageMapper;

    /**
     * 备注常量  首次翻倍
     */
    @Autowired
    private DictRemarkMapper dictRemarkMapper;

    /**
     * 药品用量单位 袋、片
     */
    @Autowired
    private DictUnitMapper dictUnitMapper;

    /**
     * 穴位常量 太阳穴
     */
    @Autowired
    private DictAcupointMapper dictAcupointMapper;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 从redis中获取系统常量
     * @return
     */
    public Map<String, Object> getAllDicts() {
        Map<String, Object> dictMap = new HashMap<>();
        //检查项目
        List<DictSysItemVo> examList = getDictItemList(DictConstant.EXAM_TYPE);
        dictMap.put(DictConstant.EXAM_TYPE,examList);
        //检验项目
        List<DictSysItemVo> labList = getDictItemList(DictConstant.LAB_TYPE);
        dictMap.put(DictConstant.LAB_TYPE,labList);
        //医嘱
        List<DictSysItemVo> adviceList = getDictItemList(DictConstant.DOCTOR_ADVICE);
        dictMap.put(DictConstant.DOCTOR_ADVICE,adviceList);
        //执行类型
        List<DictSysItemVo> execTypeList = getDictItemList(DictConstant.EXEC_TYPE);
        dictMap.put(DictConstant.EXEC_TYPE,execTypeList);
        //药品成分
        List<DictSysItemVo> ingrList = getDictItemList(DictConstant.DRUG_INGR);
        dictMap.put(DictConstant.DRUG_INGR,ingrList);
        //治疗项目
        List<DictSysItemVo> extList = getDictItemList(DictConstant.EXT_TYPE);
        dictMap.put(DictConstant.EXT_TYPE,extList);
        //使用频率
        List<DictFrequencyVo> frequencyList = getFrequencyList();
        dictMap.put(DictConstant.FREQUENCY,frequencyList);
        //药品用法
        List<DictUsageVo> usageList = getUsageList();
        dictMap.put(DictConstant.USAGE,usageList);
        //备注常量
        List<DictRemarkVo> remarkList = getRemarkList();
        dictMap.put(DictConstant.REMARK,remarkList);
        //药品用量单位
        List<DictUnitVo> unitList = getUnitList();
        dictMap.put(DictConstant.UNIT,unitList);

        return dictMap;
    }

    /**
     * 区域树
     * @return
     */
    public List<DictRegionTreeVo> getRegionTreeList() {
        String redisKey = String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.REGION);
        if (redisUtil.hasKey(redisKey)) {
            return (List<DictRegionTreeVo>) redisUtil.get(redisKey);
        }
        // 缓存没有命中（为 null），进入数据库查询逻辑
        List<DictRegionTreeVo> dbList = getDBRegionTreeList();
        if (CollectionUtils.isEmpty(dbList)) {
            // 缓存空集合，但不是 null
            redisUtil.set(redisKey, Collections.emptyList(), CommonConstant.WEEK_1);
            return Collections.emptyList();
        }
        redisUtil.set(redisKey, dbList, CommonConstant.WEEK_1);
        return dbList;
    }


    /**
     * 列表查询
     * @return
     */
    public List<DictRegionTreeVo> getDBRegionTreeList() {
        List<DictRegion> regionList = getRegionList();
        List<DictRegionTreeVo> dictRegionTreeList = new ArrayList<>();
        for (DictRegion dictRegion: regionList) {
            DictRegionTreeVo dictRegionTree = new DictRegionTreeVo();
            dictRegionTree.setCode(dictRegion.getCode());
            dictRegionTree.setPcode(dictRegion.getPcode());
            dictRegionTree.setName(dictRegion.getName());
            dictRegionTree.setLevel(dictRegion.getLevel());
            dictRegionTreeList.add(dictRegionTree);
        }
        return TreeParser.getTreeList(CommonConstant.PARENT_ID, dictRegionTreeList);
    }

    @Autowired
    private SpecDictMapper specDictMapper;

    /**
     * 穴位
     * @return
     */
    public List<DictAcupointTreeVo> getAcupointTreeList(String acupointText) {
        if(StringUtil.isNotBlank(acupointText)){
            return getAcupointTreeListByText(acupointText);
        }else {
            return getAllAcupointTreeList();
        }
    }

    public List<DictAcupointTreeVo> getAcupointTreeListByText(String acupointText) {
        List<DictAcupoint> acupointList = specDictMapper.getDBAcupointTreeListByText(acupointText);
        if(CollectionUtils.isEmpty(acupointList)){
            return null;
        }
        List<DictAcupointTreeVo> dictAcupointTreeVoList = new ArrayList<>();
        for (DictAcupoint dictAcupoint: acupointList) {
            DictAcupointTreeVo dictAcupointTreeVo = new DictAcupointTreeVo();
            dictAcupointTreeVo.setAcupointCode(dictAcupoint.getAcupointCode());
            dictAcupointTreeVo.setAcupointText(dictAcupoint.getAcupointText());
            dictAcupointTreeVo.setAcupointPinyin(dictAcupoint.getAcupointPinyin());
            dictAcupointTreeVo.setAcupointType(dictAcupoint.getAcupointType());
            dictAcupointTreeVo.setLocationType(dictAcupoint.getLocationType());
            dictAcupointTreeVo.setSideType(dictAcupoint.getSideType());
            dictAcupointTreeVo.setSortNum(dictAcupoint.getSortNum());
            dictAcupointTreeVo.setParentCode(dictAcupoint.getParentCode());
            dictAcupointTreeVoList.add(dictAcupointTreeVo);
        }
        return TreeParser.getTreeList(String.valueOf(CommonConstant.PARENT_ID), dictAcupointTreeVoList);
    }

    public List<DictAcupointTreeVo> getAllAcupointTreeList(){
        String redisKey = String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.ACUPOINT);
        if (redisUtil.hasKey(redisKey)) {
            return (List<DictAcupointTreeVo>) redisUtil.get(redisKey);
        }
        // 缓存没有命中（为 null），进入数据库查询逻辑
        List<DictAcupointTreeVo> dbList = getDBAcupointTreeList();
        if (CollectionUtils.isEmpty(dbList)) {
            // 缓存空集合，但不是 null
            redisUtil.set(redisKey, Collections.emptyList(), CommonConstant.WEEK_1);
            return Collections.emptyList();
        }
        redisUtil.set(redisKey, dbList, CommonConstant.WEEK_1);
        return dbList;
    }

    /**
     * 根据穴位名称查询穴位列表
     * @param acupointText
     * @return
     */
    public List<DictAcupointVo> getAcupointListByText(String acupointText) {
        if(StringUtil.isBlank(acupointText)){
            return Collections.EMPTY_LIST;
        }
        List<DictAcupoint> acupointList = getDBAcupointList(acupointText);
        if(CollectionUtils.isEmpty(acupointList)){
            return Collections.EMPTY_LIST;
        }
        // 将实体列表转换为DTO列表
        return acupointList.stream().map(acupoint -> {
            DictAcupointVo dictAcupointVo = new DictAcupointVo();
            BeanUtils.copyProperties(acupoint, dictAcupointVo);
            return dictAcupointVo;
        }).collect(Collectors.toList());
    }


    /**
     * 列表查询
     * @return
     */
    public List<DictAcupointTreeVo> getDBAcupointTreeList() {
        List<DictAcupoint> acupointList = getDBAcupointList(null);
        List<DictAcupointTreeVo> dictAcupointTreeVoList = new ArrayList<>();
        for (DictAcupoint dictAcupoint: acupointList) {
            DictAcupointTreeVo dictAcupointTreeVo = new DictAcupointTreeVo();
            dictAcupointTreeVo.setAcupointCode(dictAcupoint.getAcupointCode());
            dictAcupointTreeVo.setAcupointText(dictAcupoint.getAcupointText());
            dictAcupointTreeVo.setAcupointPinyin(dictAcupoint.getAcupointPinyin());
            dictAcupointTreeVo.setAcupointType(dictAcupoint.getAcupointType());
            dictAcupointTreeVo.setLocationType(dictAcupoint.getLocationType());
            dictAcupointTreeVo.setSideType(dictAcupoint.getSideType());
            dictAcupointTreeVo.setSortNum(dictAcupoint.getSortNum());
            dictAcupointTreeVo.setParentCode(dictAcupoint.getParentCode());
            dictAcupointTreeVoList.add(dictAcupointTreeVo);
        }
        return TreeParser.getTreeList(String.valueOf(CommonConstant.PARENT_ID), dictAcupointTreeVoList);
    }



    /**
     * 穴位
     * @return
     */
    public List<DictAcupoint> getAcupointList() {
        String redisKey = String.format(RedisKeyPrefix.DICT, DictConstant.ACUPOINT);
        if (redisUtil.hasKey(redisKey)) {
            return (List<DictAcupoint>) redisUtil.get(redisKey);
        }
        // 缓存没有命中（为 null），进入数据库查询逻辑
        List<DictAcupoint> dbList = getDBAcupointList(null);
        if (CollectionUtils.isEmpty(dbList)) {
            // 缓存空集合，但不是 null
            redisUtil.set(redisKey, Collections.emptyList(), CommonConstant.WEEK_1);
            return Collections.emptyList();
        }
        redisUtil.set(redisKey, dbList, CommonConstant.WEEK_1);
        return dbList;
    }



    /**
     * 从数据库中查询穴位
     * @return
     */
    public List<DictAcupoint> getDBAcupointList(String acupointText) {
        DictAcupointExample example = new DictAcupointExample();
        DictAcupointExample.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotBlank(acupointText)){
            criteria.andAcupointTextLike("%"+acupointText+"%");
        }
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        return dictAcupointMapper.selectByExample(example);
    }



    /**
     * 用量单位
     * @return
     */
    private List<DictUnitVo> getUnitList() {
        String redisKey = String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.UNIT);
        if (redisUtil.hasKey(redisKey)) {
            return (List<DictUnitVo>) redisUtil.get(redisKey);
        }
        // 缓存没有命中（为 null），进入数据库查询逻辑
        List<DictUnitVo> dbList = getDBUnitList();
        if (CollectionUtils.isEmpty(dbList)) {
            // 缓存空集合，但不是 null
            redisUtil.set(redisKey, Collections.emptyList(), CommonConstant.WEEK_1);
            return Collections.emptyList();
        }
        redisUtil.set(redisKey, dbList, CommonConstant.WEEK_1);
        return dbList;
    }

    public List<DictUnitVo> getDBUnitList(){
        DictUnitExample example = new DictUnitExample();
        DictUnitExample.Criteria criteria = example.createCriteria();
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        List<DictUnit> dictUnitList = dictUnitMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(dictUnitList)){
            return Collections.emptyList();
        }
        return dictUnitList.stream().map(dictUnit -> {
            DictUnitVo dictUnitVo = new DictUnitVo();
            BeanUtils.copyProperties(dictUnit, dictUnitVo);
            return dictUnitVo;
        }).collect(Collectors.toList());
    }

    /**
     * 备注
     * @return
     */
    private List<DictRemarkVo> getRemarkList() {
        String redisKey = String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.REMARK);
        if (redisUtil.hasKey(redisKey)) {
            return (List<DictRemarkVo>) redisUtil.get(redisKey);
        }
        // 缓存没有命中（为 null），进入数据库查询逻辑
        List<DictRemarkVo> dbList = getDBRemarkList();
        if (CollectionUtils.isEmpty(dbList)) {
            // 缓存空集合，但不是 null
            redisUtil.set(redisKey, Collections.emptyList(), CommonConstant.WEEK_1);
            return Collections.emptyList();
        }
        redisUtil.set(redisKey, dbList, CommonConstant.WEEK_1);
        return dbList;
    }

    public List<DictRemarkVo> getDBRemarkList(){
        DictRemarkExample example = new DictRemarkExample();
        DictRemarkExample.Criteria criteria = example.createCriteria();
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        List<DictRemark> dictRemarkList = dictRemarkMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(dictRemarkList)){
            return Collections.emptyList();
        }
        return dictRemarkList.stream().map(dictRemark -> {
            DictRemarkVo dictRemarkVo = new DictRemarkVo();
            BeanUtils.copyProperties(dictRemark, dictRemarkVo);
            return dictRemarkVo;
        }).collect(Collectors.toList());
    }



    /**
     * 药品用法
     * @return
     */
    public List<DictUsageVo> getUsageList() {
        String redisKey = String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.USAGE);
        if (redisUtil.hasKey(redisKey)) {
            return (List<DictUsageVo>) redisUtil.get(redisKey);
        }
        // 缓存没有命中（为 null），进入数据库查询逻辑
        List<DictUsageVo> dbList = getDBUsageList();
        if (CollectionUtils.isEmpty(dbList)) {
            // 缓存空集合，但不是 null
            redisUtil.set(redisKey, Collections.emptyList(), CommonConstant.WEEK_1);
            return Collections.emptyList();
        }
        redisUtil.set(redisKey, dbList, CommonConstant.WEEK_1);
        return dbList;
    }

    public List<DictUsageVo> getDBUsageList(){
        DictUsageExample example = new DictUsageExample();
        DictUsageExample.Criteria criteria = example.createCriteria();
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        List<DictUsage> dictUsageList = dictUsageMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(dictUsageList)){
            return Collections.emptyList();
        }
        return dictUsageList.stream().map(dictUsage -> {
            DictUsageVo dictUsageVo = new DictUsageVo();
            BeanUtils.copyProperties(dictUsage, dictUsageVo);
            return dictUsageVo;
        }).collect(Collectors.toList());
    }

    /**
     * 使用频率
     * @return
     */
    private List<DictFrequencyVo> getFrequencyList() {
        String redisKey = String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.FREQUENCY);
        if (redisUtil.hasKey(redisKey)) {
            return (List<DictFrequencyVo>) redisUtil.get(redisKey);
        }
        // 缓存没有命中（为 null），进入数据库查询逻辑
        List<DictFrequencyVo> dbList = getDBFrequencyList();
        if (CollectionUtils.isEmpty(dbList)) {
            // 缓存空集合，但不是 null
            redisUtil.set(redisKey, Collections.emptyList(), CommonConstant.WEEK_1);
            return Collections.emptyList();
        }
        redisUtil.set(redisKey, dbList, CommonConstant.WEEK_1);
        return dbList;
    }


    public List<DictFrequencyVo> getDBFrequencyList(){
        DictFrequencyExample example = new DictFrequencyExample();
        DictFrequencyExample.Criteria criteria = example.createCriteria();
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        List<DictFrequency> frequencyList = dictFrequencyMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(frequencyList)){
            return Collections.emptyList();
        }
        return frequencyList.stream().map(dictFrequency -> {
            DictFrequencyVo dictFrequencyVo = new DictFrequencyVo();
            BeanUtils.copyProperties(dictFrequency, dictFrequencyVo);
            return dictFrequencyVo;
        }).collect(Collectors.toList());
    }

    /**
     * 查询字典
     * @return
     */
    public List<DictSysItemVo> getDictItemList(String dictCode,List<String> itemCodeList) {
        List<DictSysItemVo> itemVoList = getDictItemList(dictCode);
        if(CollectionUtils.isEmpty(itemVoList)){
            return Collections.EMPTY_LIST;
        }
        return itemVoList.stream()
                .filter(vo -> itemCodeList.contains(vo.getItemCode()))
                .collect(Collectors.toList());
    }

    /**
     * 检查项目
     * @return
     */
    public List<DictSysItemVo> getDictItemList(String dictCode) {
        String redisKey = String.format(RedisKeyPrefix.DICT_SYS_CODE, dictCode);
        if (redisUtil.hasKey(redisKey)) {
            return (List<DictSysItemVo>) redisUtil.get(redisKey);
        }
        // 缓存没有命中（为 null），进入数据库查询逻辑
        List<DictSysItemVo> dbList = getDBDictItemList(dictCode);
        if (CollectionUtils.isEmpty(dbList)) {
            // 缓存空集合，但不是 null
            redisUtil.set(redisKey, Collections.emptyList(), CommonConstant.WEEK_1);
            return Collections.emptyList();
        }
        redisUtil.set(redisKey, dbList, CommonConstant.WEEK_1);
        return dbList;
    }


    /**
     * 获取系统常量
     * @param dictCode
     * @return
     */
    public List<DictSysItemVo> getDBDictItemList(String dictCode){
        if(StringUtil.isBlank(dictCode)){
            return Collections.emptyList();
        }
        DictSysItemExample example = new DictSysItemExample();
        DictSysItemExample.Criteria criteria = example.createCriteria();
        criteria.andDictCodeEqualTo(dictCode);
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        List<DictSysItem> dictItemList = dictSysItemMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(dictItemList)){
            return Collections.emptyList();
        }
        return dictItemList.stream().map(dictItem -> {
            DictSysItemVo dictSysItemVo = new DictSysItemVo();
            BeanUtils.copyProperties(dictItem, dictSysItemVo);
            return dictSysItemVo;
        }).collect(Collectors.toList());
    }


    /**
     * 频率
     * @param frequencyCode
     * @return
     */
    public DictFrequencyVo getCacheFrequency(String frequencyCode){
        if(StringUtil.hasBlank(frequencyCode)){
            return null;
        }
        List<DictFrequencyVo> frequencyList = getFrequencyList();
        if(CollectionUtils.isEmpty(frequencyList)){
            return null;
        }
        for(DictFrequencyVo frequency : frequencyList){
            if(frequencyCode.equals(frequency.getFrequencyCode())){
                return frequency;
            }
        }
        return null;
    }


    /**
     * 服用方式
     * @param rxType
     * @param usageCode
     * @return
     */
    public DictUsageVo getUsageValue(String rxType,String usageCode){
        if(StringUtil.hasBlank(rxType,usageCode)){
            return null;
        }
        List<DictUsageVo> usageList = getUsageList();
        if(CollectionUtils.isEmpty(usageList)){
            return null;
        }
        for(DictUsageVo usageVo : usageList){
            String rxTypeStr = usageVo.getRxType();
            if (rxTypeStr != null
                    && Arrays.asList(rxTypeStr.split(",")).contains(rxType)
                    && usageCode.equals(usageVo.getUsageCode())) {
                return usageVo;
            }
        }
        return null;
    }

    /**
     * 服用方式
     * @param usageCode
     * @return
     */
    public boolean isUsageValue(String usageCode){
        if(StringUtil.hasBlank(usageCode)){
            return false;
        }
        List<DictUsageVo> usageList = getUsageList();
        if(CollectionUtils.isEmpty(usageList)){
            return false;
        }
        for(DictUsageVo usageVo : usageList){
            if (usageCode.equals(usageVo.getUsageCode())) {
                return true;
            }

        }
        return false;
    }

    /**
     * 备注
     * @param rxType
     * @param remarkCode
     * @return
     */
    public boolean isRemarkValue(String rxType,String remarkCode){
        if(StringUtil.hasBlank(rxType,remarkCode)){
            return false;
        }
        List<DictRemarkVo> remarkList = getRemarkList();
        if(CollectionUtils.isEmpty(remarkList)){
            return false;
        }
        for(DictRemarkVo dictRemarkVo : remarkList){
            if(rxType.equals(dictRemarkVo.getRxType()) && remarkCode.equals(dictRemarkVo.getRemarkCode())){
                return true;
            }
        }
        return false;
    }

    /**
     * 获取穴位信息
     * @param acupointCode
     * @return
     */
    public DictAcupoint getCacheAcupoint(String acupointCode) {
        if(StringUtil.isBlank(acupointCode)){
            return null;
        }
        List<DictAcupoint> acupointList = getAcupointList();
        if(CollectionUtils.isEmpty(acupointList)){
            return null;
        }
        for(DictAcupoint dictAcupoint : acupointList){
            if(AcupointTypeEnum.ACUPOINT.getValue().equals(dictAcupoint.getAcupointType()) && acupointCode.equals(dictAcupoint.getAcupointCode())){
                return dictAcupoint;
            }
        }
        return null;
    }

    /**
     * 穴位
     * @param acupointCode
     * @return
     */
    public boolean isAcupointValue(String acupointCode) {
        if(StringUtil.hasBlank(acupointCode)){
            return false;
        }
        List<DictAcupoint> dictAcupointList = getAcupointList();
        if(CollectionUtils.isEmpty(dictAcupointList)){
            return false;
        }
        for(DictAcupoint acupoint : dictAcupointList){
            if(acupointCode.equals(acupoint.getAcupointCode())){
                return true;
            }
        }
        return false;
    }
    /**
     * 检查单位编码
     * @param unitType
     * @param unitCode
     * @return
     */
    public DictUnitVo getUnitValue(String unitType,String unitCode){
        if(StringUtil.hasBlank(unitType,unitCode)){
            return null;
        }
        List<DictUnitVo> unitList = getUnitList();
        if(CollectionUtils.isEmpty(unitList)){
            return null;
        }
        for(DictUnitVo unit : unitList){
            if(unitType.equals(unit.getUnitType()) && unitCode.equals(unit.getUnitCode())){
                return unit;
            }
        }
        return null;
    }

    /**
     * 检查单位编码
     * @param unitType
     * @param unitCode
     * @return
     */
    public boolean isUnitValue(String unitType,String unitCode){
        if(StringUtil.hasBlank(unitType,unitCode)){
            return false;
        }
        List<DictUnitVo> unitList = getUnitList();
        if(CollectionUtils.isEmpty(unitList)){
            return false;
        }
        for(DictUnitVo unit : unitList){
            if(unitType.equals(unit.getUnitType()) && unitCode.equals(unit.getUnitCode())){
                return true;
            }
        }
        return false;
    }


    /**
     * 检查常量值是否正确
     * @param dictCode
     * @param itemCode
     * @return
     */
    public boolean isDictValue(String dictCode, String itemCode){
        if(StringUtil.hasBlank(dictCode,itemCode)){
            return false;
        }
        List<DictSysItemVo> itemList = getDictItemList(dictCode);
        if(CollectionUtils.isEmpty(itemList)){
            return false;
        }
        for(DictSysItemVo item : itemList){
            if(itemCode.equals(item.getItemCode())){
                return true;
            }
        }
        return false;
    }



    /**
     * 分页查询穴位常量
     * @param acupointName 穴位名称（模糊搜索）
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    public PageInfo<DictAcupoint> getAcupointListByCondition(String acupointName, Integer pageNo, Integer pageSize) {
        DictAcupointExample example = new DictAcupointExample();
        DictAcupointExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(acupointName)) {
            criteria.andAcupointTextLike("%" + acupointName + "%");
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = dictAcupointMapper.countByExample(example);
        if (totalCount == null || totalCount == 0) {
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo, pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("create_time desc");
        List<DictAcupoint> acupointList = dictAcupointMapper.selectByExample(example);
        return new PageInfo<>(totalCount, acupointList);
    }

    /**
     * 添加穴位常量
     * @param dictAcupoint 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void addAcupoint(DictAcupoint dictAcupoint) {
        validateAcupoint(dictAcupoint);
        DictAcupoint dbAcupoint = getAcupointByCode(dictAcupoint.getAcupointCode());
        if (dbAcupoint != null) {
            throw new BusinessFailException("编码已存在");
        }
        dictAcupoint.setIsDelete(YesOrNoEnum.NO.getValue());
        dictAcupoint.setIsEnabled(YesOrNoEnum.YES.getValue());
        dictAcupoint.setCreateTime(new Date());
        dictAcupointMapper.insertSelective(dictAcupoint);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.ACUPOINT));
        redisUtil.del(String.format(RedisKeyPrefix.DICT, DictConstant.ACUPOINT));
    }

    /**
     * 校验穴位常量
     * @param dictAcupoint 实体
     */
    public void validateAcupoint(DictAcupoint dictAcupoint) {
        // 可加入唯一性、非空校验
        if (StringUtil.isBlank(dictAcupoint.getAcupointCode())) {
            throw new BusinessFailException("穴位编码不能为空");
        }
        if (StringUtil.isBlank(dictAcupoint.getAcupointText())) {
            throw new BusinessFailException("穴位名称不能为空");
        }
        if (StringUtil.isBlank(dictAcupoint.getAcupointType())) {
            throw new BusinessFailException("穴位类型不能为空");
        }
        if(!AcupointTypeEnum.isValue(dictAcupoint.getAcupointType())){
            throw new BusinessFailException("穴位类型错误");
        }
        if(AcupointTypeEnum.ACUPOINT.getValue().equals(dictAcupoint.getAcupointType())){
            //穴位
            if (StringUtil.isBlank(dictAcupoint.getSideType())) {
                throw new BusinessFailException("穴位侧别类型不能为空");
            }
            if(!AcupointSideTypeEnum.isValue(dictAcupoint.getSideType())){
                throw new BusinessFailException("穴位侧别类型错误");
            }
        }else {
            //经络
            if (StringUtil.isBlank(dictAcupoint.getLocationType())) {
                throw new BusinessFailException("位置类型不能为空");
            }
            if(!AcupointLocationTypeEnum.isValue(dictAcupoint.getLocationType())){
                throw new BusinessFailException("位置类型错误");
            }
        }
        if(StringUtil.isBlank(dictAcupoint.getParentCode())){
            dictAcupoint.setParentCode(String.valueOf(CommonConstant.DEFAULT_0));
        }
        dictAcupoint.setAcupointPinyin(PinyinUtils.getFirstLetters(dictAcupoint.getAcupointText()));
        if (dictAcupoint.getSortNum() == null || dictAcupoint.getSortNum() < 0) {
            dictAcupoint.setSortNum(CommonConstant.DEFAULT_0);
        }
    }

    /**
     * 根据主键获取穴位信息
     * @param acupointCode 主键
     * @return 实体
     */
    public DictAcupoint getAcupointByCode(String acupointCode) {
        if (StringUtil.isBlank(acupointCode)) {
            return null;
        }
        DictAcupoint dictAcupoint = dictAcupointMapper.selectByPrimaryKey(acupointCode);
        if (dictAcupoint == null || YesOrNoEnum.YES.getValue().equals(dictAcupoint.getIsDelete())) {
            return null;
        }
        return dictAcupoint;
    }

    /**
     * 修改穴位常量
     * @param dictAcupoint 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateAcupoint(DictAcupoint dictAcupoint) {
        validateAcupoint(dictAcupoint);
        DictAcupoint dbAcupoint = getAcupointByCode(dictAcupoint.getAcupointCode());
        if (dbAcupoint == null) {
            throw new BusinessFailException("穴位不存在");
        }
        dictAcupoint.setUpdateTime(new Date());
        dictAcupointMapper.updateByPrimaryKeySelective(dictAcupoint);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.ACUPOINT));
        redisUtil.del(String.format(RedisKeyPrefix.DICT, DictConstant.ACUPOINT));
    }

    /**
     * 删除穴位常量（逻辑删除）
     * @param dictAcupoint 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteAcupoint(DictAcupoint dictAcupoint) {
        DictAcupoint dbAcupoint = getAcupointByCode(dictAcupoint.getAcupointCode());
        if (dbAcupoint == null) {
            throw new BusinessFailException("穴位不存在");
        }
        dictAcupointMapper.deleteByPrimaryKey(dbAcupoint.getAcupointCode());
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.ACUPOINT));
        redisUtil.del(String.format(RedisKeyPrefix.DICT, DictConstant.ACUPOINT));
    }

    /**
     * 启用穴位常量
     * @param dictAcupoint 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableAcupoint(DictAcupoint dictAcupoint) {
        DictAcupoint dbAcupoint = getAcupointByCode(dictAcupoint.getAcupointCode());
        if (dbAcupoint == null) {
            throw new BusinessFailException("穴位不存在");
        }
        if (YesOrNoEnum.YES.getValue().equals(dbAcupoint.getIsEnabled())) {
            return;
        }
        dbAcupoint.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbAcupoint.setUpdateUserId(dictAcupoint.getUpdateUserId());
        dbAcupoint.setUpdateBy(dictAcupoint.getUpdateBy());
        dbAcupoint.setUpdateTime(new Date());
        dictAcupointMapper.updateByPrimaryKeySelective(dbAcupoint);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.ACUPOINT));
        redisUtil.del(String.format(RedisKeyPrefix.DICT, DictConstant.ACUPOINT));
    }

    /**
     * 停用穴位常量
     * @param dictAcupoint 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableAcupoint(DictAcupoint dictAcupoint) {
        DictAcupoint dbAcupoint = getAcupointByCode(dictAcupoint.getAcupointCode());
        if (dbAcupoint == null) {
            throw new BusinessFailException("穴位不存在");
        }
        if (YesOrNoEnum.NO.getValue().equals(dbAcupoint.getIsEnabled())) {
            return;
        }
        dbAcupoint.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbAcupoint.setUpdateUserId(dictAcupoint.getUpdateUserId());
        dbAcupoint.setUpdateBy(dictAcupoint.getUpdateBy());
        dbAcupoint.setUpdateTime(new Date());
        dictAcupointMapper.updateByPrimaryKeySelective(dbAcupoint);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.ACUPOINT));
        redisUtil.del(String.format(RedisKeyPrefix.DICT, DictConstant.ACUPOINT));
    }


    /**
     * 分页查询药品用量单位常量
     * @param unitCode 用量单位（模糊搜索）
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    public PageInfo<DictUnit> getUnitListByCondition(String unitCode, Integer pageNo, Integer pageSize) {
        DictUnitExample example = new DictUnitExample();
        DictUnitExample.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotBlank(unitCode)){
            criteria.andUnitCodeEqualTo(unitCode);
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = dictUnitMapper.countByExample(example);
        if (totalCount == null || totalCount == 0) {
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo, pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("sort_num asc");
        List<DictUnit> unitList = dictUnitMapper.selectByExample(example);
        return new PageInfo<>(totalCount, unitList);
    }

    /**
     * 添加药品用量单位常量
     * @param dictUnit 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void addUnit(DictUnit dictUnit) {
        validateUnit(dictUnit);
        DictUnit dbDictUnit = getUnitByCode(dictUnit.getUnitCode());
        if (dbDictUnit != null) {
            throw new BusinessFailException("编码已存在");
        }
        dictUnit.setIsDelete(YesOrNoEnum.NO.getValue());
        dictUnit.setIsEnabled(YesOrNoEnum.YES.getValue());
        dictUnit.setCreateTime(new Date());
        dictUnitMapper.insertSelective(dictUnit);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.UNIT));
    }

    /**
     * 校验用量单位
     * @param dictUnit 实体
     */
    public void validateUnit(DictUnit dictUnit) {
        //可添加唯一性、非空等校验逻辑
        if (StringUtil.isBlank(dictUnit.getUnitCode())) {
            throw new BusinessFailException("单位编码不能为空");
        }
        if(StringUtil.isBlank(dictUnit.getUnitType())){
            throw new BusinessFailException("单位类型不能为空");
        }
        if(!UnitTypeEnum.isValue(dictUnit.getUnitType())){
            throw new BusinessFailException("单位类型错误");
        }
        if (StringUtil.isBlank(dictUnit.getUnitText())) {
            throw new BusinessFailException("单位名称不能为空");
        }
        // 排序号为空或非法时设为默认值
        if (dictUnit.getSortNum() == null || dictUnit.getSortNum() < 0) {
            dictUnit.setSortNum(CommonConstant.DEFAULT_0);
        }
    }

    /**
     * 根据主键查询药品用量单位
     * @param unitCode 主键
     * @return 实体
     */
    public DictUnit getUnitByCode(String unitCode) {
        if (StringUtil.isBlank(unitCode)) {
            return null;
        }
        DictUnit dictUnit = dictUnitMapper.selectByPrimaryKey(unitCode);
        if (dictUnit == null || YesOrNoEnum.YES.getValue().equals(dictUnit.getIsDelete())) {
            return null;
        }
        return dictUnit;
    }

    /**
     * 修改药品用量单位常量
     * @param dictUnit 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateUnit(DictUnit dictUnit) {
        validateUnit(dictUnit);
        DictUnit dbDictUnit = getUnitByCode(dictUnit.getUnitCode());
        if (dbDictUnit == null) {
            throw new BusinessFailException("单位不存在");
        }
        dictUnit.setUpdateTime(new Date());
        dictUnitMapper.updateByPrimaryKeySelective(dictUnit);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.UNIT));
    }

    /**
     * 删除药品用量单位常量（逻辑删除）
     * @param dictUnit 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteUnit(DictUnit dictUnit) {
        DictUnit dbDictUnit = getUnitByCode(dictUnit.getUnitCode());
        if (dbDictUnit == null) {
            throw new BusinessFailException("单位不存在");
        }
        dictUnitMapper.deleteByPrimaryKey(dbDictUnit.getUnitCode());
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.UNIT));
    }

    /**
     * 启用药品用量单位常量
     * @param dictUnit 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableUnit(DictUnit dictUnit) {
        DictUnit dbDictUnit = getUnitByCode(dictUnit.getUnitCode());
        if (dbDictUnit == null) {
            throw new BusinessFailException("单位不存在");
        }
        if (YesOrNoEnum.YES.getValue().equals(dbDictUnit.getIsEnabled())) {
            return;
        }
        dbDictUnit.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbDictUnit.setUpdateUserId(dictUnit.getUpdateUserId());
        dbDictUnit.setUpdateBy(dictUnit.getUpdateBy());
        dbDictUnit.setUpdateTime(new Date());
        dictUnitMapper.updateByPrimaryKeySelective(dbDictUnit);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.UNIT));
    }

    /**
     * 停用药品用量单位常量
     * @param dictUnit 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableUnit(DictUnit dictUnit) {
        DictUnit dbDictUnit = getUnitByCode(dictUnit.getUnitCode());
        if (dbDictUnit == null) {
            throw new BusinessFailException("单位不存在");
        }
        if (YesOrNoEnum.NO.getValue().equals(dbDictUnit.getIsEnabled())) {
            return;
        }
        dbDictUnit.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbDictUnit.setUpdateUserId(dictUnit.getUpdateUserId());
        dbDictUnit.setUpdateBy(dictUnit.getUpdateBy());
        dbDictUnit.setUpdateTime(new Date());
        dictUnitMapper.updateByPrimaryKeySelective(dbDictUnit);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.UNIT));
    }

    /**
     * 分页查询处方备注常量
     * @param remarkText 备注内容（模糊搜索）
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    public PageInfo<DictRemark> getRemarkListByCondition(String remarkText, Integer pageNo, Integer pageSize) {
        DictRemarkExample example = new DictRemarkExample();
        DictRemarkExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(remarkText)) {
            criteria.andRemarkTextLike("%" + remarkText + "%");
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = dictRemarkMapper.countByExample(example);
        if (totalCount == null || totalCount == 0) {
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo, pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("sort_num asc");

        List<DictRemark> remarkList = dictRemarkMapper.selectByExample(example);
        return new PageInfo<>(totalCount, remarkList);
    }

    /**
     * 添加处方备注常量
     * @param dictRemark 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void addRemark(DictRemark dictRemark) {
        validateRemark(dictRemark);
        DictRemark dbDictRemark = getRemarkByCode(dictRemark.getRemarkCode());
        if (dbDictRemark != null) {
            throw new BusinessFailException("编码已存在");
        }
        dictRemark.setIsDelete(YesOrNoEnum.NO.getValue());
        dictRemark.setIsEnabled(YesOrNoEnum.YES.getValue());
        dictRemark.setCreateTime(new Date());
        dictRemarkMapper.insertSelective(dictRemark);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.REMARK));
    }

    /**
     * 校验实体
     * @param dictRemark 实体
     */
    public void validateRemark(DictRemark dictRemark) {
        // 可扩展字段非空、唯一性等校验逻辑
        if (StringUtil.isBlank(dictRemark.getRemarkCode())) {
            throw new BusinessFailException("备注编码不能为空");
        }
        if(StringUtil.isBlank(dictRemark.getRxType())){
            throw new BusinessFailException("处方类型不能为空");
        }
        if(!RxTypeEnum.isValue(dictRemark.getRxType())){
            throw new BusinessFailException("处方类型错误");
        }

        if (StringUtil.isBlank(dictRemark.getRemarkText())) {
            throw new BusinessFailException("备注名称不能为空");
        }
        // 排序号为空或非法时设为默认值
        if (dictRemark.getSortNum() == null || dictRemark.getSortNum() < 0) {
            dictRemark.setSortNum(CommonConstant.DEFAULT_0);
        }
    }

    /**
     * 根据主键获取处方备注
     * @param remarkCode 主键
     * @return 实体
     */
    public DictRemark getRemarkByCode(String remarkCode) {
        if (StringUtil.isBlank(remarkCode)) {
            return null;
        }
        DictRemark dictRemark = dictRemarkMapper.selectByPrimaryKey(remarkCode);
        if (dictRemark == null || YesOrNoEnum.YES.getValue().equals(dictRemark.getIsDelete())) {
            return null;
        }
        return dictRemark;
    }

    /**
     * 修改处方备注常量
     * @param dictRemark 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateRemark(DictRemark dictRemark) {
        validateRemark(dictRemark);
        DictRemark dbDictRemark = getRemarkByCode(dictRemark.getRemarkCode());
        if (dbDictRemark == null) {
            throw new BusinessFailException("备注不存在");
        }
        dictRemark.setUpdateTime(new Date());
        dictRemarkMapper.updateByPrimaryKeySelective(dictRemark);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.REMARK));
    }

    /**
     * 删除处方备注常量（逻辑删除）
     * @param dictRemark 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteRemark(DictRemark dictRemark) {
        DictRemark dbDictRemark = getRemarkByCode(dictRemark.getRemarkCode());
        if (dbDictRemark == null) {
            throw new BusinessFailException("备注不存在");
        }
        dictRemarkMapper.deleteByPrimaryKey(dbDictRemark.getRemarkCode());
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.REMARK));
    }

    /**
     * 启用处方备注
     * @param dictRemark 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableRemark(DictRemark dictRemark) {
        DictRemark dbDictRemark = getRemarkByCode(dictRemark.getRemarkCode());
        if (dbDictRemark == null) {
            throw new BusinessFailException("备注不存在");
        }
        if (YesOrNoEnum.YES.getValue().equals(dbDictRemark.getIsEnabled())) {
            return;
        }
        dbDictRemark.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbDictRemark.setUpdateUserId(dictRemark.getUpdateUserId());
        dbDictRemark.setUpdateBy(dictRemark.getUpdateBy());
        dbDictRemark.setUpdateTime(new Date());
        dictRemarkMapper.updateByPrimaryKeySelective(dbDictRemark);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.REMARK));
    }

    /**
     * 停用处方备注
     * @param dictRemark 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableRemark(DictRemark dictRemark) {
        DictRemark dbDictRemark = getRemarkByCode(dictRemark.getRemarkCode());
        if (dbDictRemark == null) {
            throw new BusinessFailException("备注不存在");
        }
        if (YesOrNoEnum.NO.getValue().equals(dbDictRemark.getIsEnabled())) {
            return;
        }
        dbDictRemark.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbDictRemark.setUpdateUserId(dictRemark.getUpdateUserId());
        dbDictRemark.setUpdateBy(dictRemark.getUpdateBy());
        dbDictRemark.setUpdateTime(new Date());
        dictRemarkMapper.updateByPrimaryKeySelective(dbDictRemark);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.REMARK));
    }


    /**
     * 分页查询药品用法常量
     * @param usageName 用法名称（模糊搜索）
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @return 分页数据
     */
    public PageInfo<DictUsage> getUsageListByCondition(String usageName, Integer pageNo, Integer pageSize) {
        DictUsageExample example = new DictUsageExample();
        DictUsageExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(usageName)) {
            criteria.andUsageNameLike("%" + usageName + "%");
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = dictUsageMapper.countByExample(example);
        if (totalCount == null || totalCount == 0) {
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo, pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("sort_num asc");
        List<DictUsage> usageList = dictUsageMapper.selectByExample(example);
        return new PageInfo<>(totalCount, usageList);
    }

    /**
     * 添加药品用法常量
     * @param dictUsage 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void addUsage(DictUsage dictUsage) {
        validateUsage(dictUsage);
        DictUsage dbDictUsage = getUsageByCode(dictUsage.getUsageCode());
        if (dbDictUsage != null) {
            throw new BusinessFailException("编码已存在");
        }
        dictUsage.setIsDelete(YesOrNoEnum.NO.getValue());
        dictUsage.setIsEnabled(YesOrNoEnum.YES.getValue());
        dictUsage.setCreateTime(new Date());
        dictUsageMapper.insertSelective(dictUsage);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.USAGE));
    }

    /**
     * 校验实体合法性
     * @param dictUsage 实体
     */
    public void validateUsage(DictUsage dictUsage) {
        // 可加入唯一性、非空校验
        if (StringUtil.isBlank(dictUsage.getUsageCode())) {
            throw new BusinessFailException("用法编码不能为空");
        }
        if (StringUtil.isBlank(dictUsage.getUsageName())) {
            throw new BusinessFailException("用法名称不能为空");
        }
        if (StringUtil.isBlank(dictUsage.getRxType())) {
            throw new BusinessFailException("处方类型不能为空");
        }
//        if(!RxTypeEnum.isValue(dictUsage.getRxType())){
//            throw new BusinessFailException("处方类型错误");
//        }
        if (StringUtil.isBlank(dictUsage.getParentCode())) {
            dictUsage.setParentCode(String.valueOf(CommonConstant.DEFAULT_0));
        }
        // 排序号为空或非法时设为默认值
        if (dictUsage.getSortNum() == null || dictUsage.getSortNum() < 0) {
            dictUsage.setSortNum(CommonConstant.DEFAULT_0);
        }
    }

    /**
     * 根据主键获取用法常量
     * @param usageCode 主键
     * @return 实体
     */
    public DictUsage getUsageByCode(String usageCode) {
        if (StringUtil.isBlank(usageCode)) {
            return null;
        }
        DictUsage dictUsage = dictUsageMapper.selectByPrimaryKey(usageCode);
        if (dictUsage == null || YesOrNoEnum.YES.getValue().equals(dictUsage.getIsDelete())) {
            return null;
        }
        return dictUsage;
    }

    /**
     * 修改药品用法常量
     * @param dictUsage 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateUsage(DictUsage dictUsage) {
        validateUsage(dictUsage);
        DictUsage dbDictUsage = getUsageByCode(dictUsage.getUsageCode());
        if (dbDictUsage == null) {
            throw new BusinessFailException("用法不存在");
        }
        dictUsage.setUpdateTime(new Date());
        dictUsageMapper.updateByPrimaryKeySelective(dictUsage);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.USAGE));
    }

    /**
     * 删除药品用法常量（逻辑删除）
     * @param dictUsage 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteUsage(DictUsage dictUsage) {
        DictUsage dbDictUsage = getUsageByCode(dictUsage.getUsageCode());
        if (dbDictUsage == null) {
            throw new BusinessFailException("用法不存在");
        }
        dictUsageMapper.deleteByPrimaryKey(dbDictUsage.getUsageCode());
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.USAGE));
    }

    /**
     * 启用药品用法
     * @param dictUsage 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableUsage(DictUsage dictUsage) {
        DictUsage dbDictUsage = getUsageByCode(dictUsage.getUsageCode());
        if (dbDictUsage == null) {
            throw new BusinessFailException("用法不存在");
        }
        if (YesOrNoEnum.YES.getValue().equals(dbDictUsage.getIsEnabled())) {
            return;
        }
        dbDictUsage.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbDictUsage.setUpdateUserId(dictUsage.getUpdateUserId());
        dbDictUsage.setUpdateBy(dictUsage.getUpdateBy());
        dbDictUsage.setUpdateTime(new Date());
        dictUsageMapper.updateByPrimaryKeySelective(dbDictUsage);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.USAGE));
    }

    /**
     * 停用药品用法
     * @param dictUsage 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableUsage(DictUsage dictUsage) {
        DictUsage dbDictUsage = getUsageByCode(dictUsage.getUsageCode());
        if (dbDictUsage == null) {
            throw new BusinessFailException("用法不存在");
        }
        if (YesOrNoEnum.NO.getValue().equals(dbDictUsage.getIsEnabled())) {
            return;
        }
        dbDictUsage.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbDictUsage.setUpdateUserId(dictUsage.getUpdateUserId());
        dbDictUsage.setUpdateBy(dictUsage.getUpdateBy());
        dbDictUsage.setUpdateTime(new Date());
        dictUsageMapper.updateByPrimaryKeySelective(dbDictUsage);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.USAGE));
    }

    /**
     * 查询系统常量列表
     * @param dictCode
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageInfo<DictSys> geDictSysListByCondition(String dictCode, Integer pageNo, Integer pageSize){
        DictSysExample example = new DictSysExample();
        DictSysExample.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotBlank(dictCode)){
            criteria.andDictCodeEqualTo(dictCode);
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = dictSysMapper.countByExample(example);
        if(StringUtil.isBlank(totalCount)){
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo,pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("sort_num asc");
        List<DictSys> dictSysList = dictSysMapper.selectByExample(example);
        return new PageInfo<>(totalCount,dictSysList);
    }

    /**
     * 添加
     * @param dictSys
     */
    @Transactional(rollbackFor = Exception.class)
    public void addDict(DictSys dictSys) {
        validateDictSys(dictSys);
        DictSys dbDictSys = getDictSysByCode(dictSys.getDictCode());
        if(dbDictSys != null){
            throw new BusinessFailException("编码已存在");
        }
        dictSys.setIsDelete(YesOrNoEnum.NO.getValue());
        dictSys.setIsEnabled(YesOrNoEnum.YES.getValue());
        dictSys.setCreateTime(new Date());
        dictSysMapper.insertSelective(dictSys);
    }

    public void validateDictSys(DictSys dictSys) {
        if(StringUtil.isBlank(dictSys.getDictCode())){
            throw new BusinessFailException("编码不能为空");
        }
        if(StringUtil.isBlank(dictSys.getDictName())){
            throw new BusinessFailException("名称不能为空");
        }
    }

    /**
     * 根据code获取常量信息
     * @param code
     * @return
     */
    public DictSys getDictSysByCode(String code){
        if(StringUtil.isBlank(code)){
            return null;
        }
        DictSys dictSys = dictSysMapper.selectByPrimaryKey(code);
        if(dictSys == null || YesOrNoEnum.YES.getValue().equals(dictSys.getIsDelete())){
            return null;
        }
        return dictSys;
    }


    /**
     * 修改
     * @param dictSys
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateDict(DictSys dictSys) {
        validateDictSys(dictSys);
        DictSys dbDictSys = getDictSysByCode(dictSys.getDictCode());
        if(dbDictSys == null){
            throw new BusinessFailException("常量不存在");
        }
        dictSys.setUpdateTime(new Date());
        dictSysMapper.updateByPrimaryKeySelective(dictSys);
    }


    /**
     * 删除
     * @param dictSys
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteDict(DictSys dictSys) {
        DictSys dbDictSys = getDictSysByCode(dictSys.getDictCode());
        if(dbDictSys == null){
            throw new BusinessFailException("常量不存在");
        }
        dbDictSys.setIsDelete(YesOrNoEnum.YES.getValue());
        dbDictSys.setUpdateUserId(dictSys.getUpdateUserId());
        dbDictSys.setUpdateBy(dictSys.getUpdateBy());
        dbDictSys.setUpdateTime(new Date());
        dictSysMapper.updateByPrimaryKeySelective(dbDictSys);
    }

    /**
     * 启用
     * @param dictSys
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableDict(DictSys dictSys) {
        DictSys dbDictSys = getDictSysByCode(dictSys.getDictCode());
        if(dbDictSys == null){
            throw new BusinessFailException("常量不存在");
        }
        if(YesOrNoEnum.YES.getValue().equals(dbDictSys.getIsEnabled())){
            return;
        }
        dbDictSys.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbDictSys.setUpdateUserId(dictSys.getUpdateUserId());
        dbDictSys.setUpdateBy(dictSys.getUpdateBy());
        dbDictSys.setUpdateTime(new Date());
        dictSysMapper.updateByPrimaryKeySelective(dbDictSys);
    }

    /**
     * 停用
     * @param dictSys
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableDict(DictSys dictSys) {
        DictSys dbDictSys = getDictSysByCode(dictSys.getDictCode());
        if(dbDictSys == null){
            throw new BusinessFailException("常量不存在");
        }
        if(YesOrNoEnum.NO.getValue().equals(dbDictSys.getIsEnabled())){
            return;
        }
        dbDictSys.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbDictSys.setUpdateUserId(dictSys.getUpdateUserId());
        dbDictSys.setUpdateBy(dictSys.getUpdateBy());
        dbDictSys.setUpdateTime(new Date());
        dictSysMapper.updateByPrimaryKeySelective(dbDictSys);
    }



    /**
     * 查询系统常量值列表
     * @param code
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageInfo<DictSysItem> geDictItemListByCondition(String code, Integer pageNo, Integer pageSize){
        if(StringUtil.isBlank(code)){
            return PageInfo.build();
        }
        DictSysItemExample example = new DictSysItemExample();
        DictSysItemExample.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotBlank(code)){
            criteria.andDictCodeEqualTo(code);
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = dictSysItemMapper.countByExample(example);
        if(StringUtil.isBlank(totalCount)){
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo,pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("sort_num asc");
        List<DictSysItem> dictSysItemList = dictSysItemMapper.selectByExample(example);
        return new PageInfo<>(totalCount,dictSysItemList);
    }

    /**
     * 添加系统常量值
     * @param dictSysItem
     */
    @Transactional(rollbackFor = Exception.class)
    public void addItem(DictSysItem dictSysItem) {
        validateDictSysItem(dictSysItem);
        dictSysItem.setIsDelete(YesOrNoEnum.NO.getValue());
        dictSysItem.setIsEnabled(YesOrNoEnum.YES.getValue());
        dictSysItem.setCreateTime(new Date());
        dictSysItemMapper.insertSelective(dictSysItem);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, dictSysItem.getDictCode()));
    }

    public void validateDictSysItem(DictSysItem dictSysItem) {
        DictSys dictSys = getDictSysByCode(dictSysItem.getDictCode());
        if(dictSys == null){
            throw new BusinessFailException("常量不存在");
        }
        if(StringUtil.isBlank(dictSysItem.getItemCode())){
            throw new BusinessFailException("编码不能为空");
        }
        if(StringUtil.isBlank(dictSysItem.getItemName())){
            throw new BusinessFailException("值不能为空");
        }
    }


    /**
     * 根据id获取常量值信息
     * @param itemId
     * @return
     */
    public DictSysItem getDictSysItemById(Long itemId){
        if(StringUtil.isBlank(itemId)){
            return null;
        }
        DictSysItem dictSysItem = dictSysItemMapper.selectByPrimaryKey(itemId);
        if(dictSysItem == null || YesOrNoEnum.YES.getValue().equals(dictSysItem.getIsDelete())){
            return null;
        }
        return dictSysItem;
    }


    /**
     * 修改系统常量值
     * @param dictSysItem
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateItem(DictSysItem dictSysItem) {
        validateDictSysItem(dictSysItem);
        DictSysItem dbDictSysItem = getDictSysItemById(dictSysItem.getItemId());
        if(dbDictSysItem == null){
            throw new BusinessFailException("常量值不存在");
        }
        dbDictSysItem.setItemCode(dictSysItem.getItemCode());
        dbDictSysItem.setItemName(dictSysItem.getItemName());
        dbDictSysItem.setSortNum(dictSysItem.getSortNum());
        dbDictSysItem.setUpdateUserId(dictSysItem.getUpdateUserId());
        dbDictSysItem.setUpdateBy(dictSysItem.getUpdateBy());
        dbDictSysItem.setUpdateTime(new Date());
        dictSysItemMapper.updateByPrimaryKeySelective(dbDictSysItem);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, dictSysItem.getDictCode()));
    }


    /**
     * 删除系统常量值
     * @param dictSysItem
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteItem(DictSysItem dictSysItem) {
        DictSysItem dbDictSysItem = getDictSysItemById(dictSysItem.getItemId());
        if(dbDictSysItem == null){
            throw new BusinessFailException("常量值不存在");
        }
        dbDictSysItem.setIsDelete(YesOrNoEnum.YES.getValue());
        dbDictSysItem.setUpdateUserId(dictSysItem.getUpdateUserId());
        dbDictSysItem.setUpdateBy(dictSysItem.getUpdateBy());
        dbDictSysItem.setUpdateTime(new Date());
        dictSysItemMapper.updateByPrimaryKeySelective(dbDictSysItem);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, dictSysItem.getDictCode()));
    }

    /**
     * 启用系统常量值
     * @param dictSysItem
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableItem(DictSysItem dictSysItem) {
        DictSysItem dbDictSysItem = getDictSysItemById(dictSysItem.getItemId());
        if(dbDictSysItem == null){
            throw new BusinessFailException("常量值不存在");
        }
        if(YesOrNoEnum.YES.getValue().equals(dbDictSysItem.getIsEnabled())){
            return;
        }
        dbDictSysItem.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbDictSysItem.setUpdateUserId(dictSysItem.getUpdateUserId());
        dbDictSysItem.setUpdateBy(dictSysItem.getUpdateBy());
        dbDictSysItem.setUpdateTime(new Date());
        dictSysItemMapper.updateByPrimaryKeySelective(dbDictSysItem);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, dictSysItem.getDictCode()));
    }

    /**
     * 停用系统常量值
     * @param dictSysItem
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableItem(DictSysItem dictSysItem) {
        DictSysItem dbDictSysItem = getDictSysItemById(dictSysItem.getItemId());
        if(dbDictSysItem == null){
            throw new BusinessFailException("常量值不存在");
        }
        if(YesOrNoEnum.NO.getValue().equals(dbDictSysItem.getIsEnabled())){
            return;
        }
        dbDictSysItem.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbDictSysItem.setUpdateUserId(dictSysItem.getUpdateUserId());
        dbDictSysItem.setUpdateBy(dictSysItem.getUpdateBy());
        dbDictSysItem.setUpdateTime(new Date());
        dictSysItemMapper.updateByPrimaryKeySelective(dbDictSysItem);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, dictSysItem.getDictCode()));
    }



    /**
     * 获取行政区域
     * @param pcode
     * @param level
     * @return
     */
    public List<DictRegion> getRegionList(Long pcode, Integer level){
        DictRegionExample example = new DictRegionExample();
        DictRegionExample.Criteria criteria = example.createCriteria();
        if(pcode != null && pcode != 0){
            criteria.andPcodeEqualTo(pcode);
        }
        if(level != null && level != 0){
            criteria.andLevelEqualTo(level);
        }
        return dictRegionMapper.selectByExample(example);
    }


    public List<DictRegion> getRegionList(){
        return getRegionList(null,null);
    }



    /**
     * 分页查询使用频率常量
     * @param frequencyName 名称（模糊搜索）
     * @param pageNo 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    public PageInfo<DictFrequency> getFrequencyListByCondition(String frequencyName, Integer pageNo, Integer pageSize) {
        DictFrequencyExample example = new DictFrequencyExample();
        DictFrequencyExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(frequencyName)) {
            criteria.andFrequencyNameLike("%" + frequencyName + "%");
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        Long totalCount = dictFrequencyMapper.countByExample(example);
        if (totalCount == null || totalCount == 0) {
            return PageInfo.build();
        }
        example.setOffset(PageUtils.getOffset(pageNo, pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("sort_num asc");
        List<DictFrequency> list = dictFrequencyMapper.selectByExample(example);
        return new PageInfo<>(totalCount, list);
    }

    /**
     * 添加使用频率常量
     * @param dictFrequency 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void addFrequency(DictFrequency dictFrequency) {
        validateFrequency(dictFrequency);
        DictFrequency dbDictFrequency = getFrequencyByCode(dictFrequency.getFrequencyCode());
        if (dbDictFrequency != null) {
            throw new BusinessFailException("编码已存在");
        }
        dictFrequency.setIsDelete(YesOrNoEnum.NO.getValue());
        dictFrequency.setIsEnabled(YesOrNoEnum.YES.getValue());
        dictFrequency.setCreateTime(new Date());
        dictFrequencyMapper.insertSelective(dictFrequency);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.FREQUENCY));
    }

    /**
     * 校验频率常量参数
     * @param dictFrequency 实体
     */
    public void validateFrequency(DictFrequency dictFrequency) {
        if (StringUtil.isBlank(dictFrequency.getFrequencyCode())) {
            throw new BusinessFailException("编码不能为空");
        }
        if (StringUtil.isBlank(dictFrequency.getFrequencyName())) {
            throw new BusinessFailException("频率名称不能为空");
        }
        if (dictFrequency.getFrequencyTimes() == null || dictFrequency.getFrequencyTimes() < CommonConstant.DEFAULT_0) {
            throw new BusinessFailException("次数必须为大于等于"+CommonConstant.DEFAULT_0);
        }
        if (StringUtil.isBlank(dictFrequency.getCycleUnit())) {
            throw new BusinessFailException("周期单位不能为空");
        }
        if(!CycleUnitEnum.isValue(dictFrequency.getCycleUnit())){
            throw new BusinessFailException("周期单位错误");
        }
        if (dictFrequency.getSortNum() == null || dictFrequency.getSortNum() < 0) {
            dictFrequency.setSortNum(CommonConstant.DEFAULT_0);
        }
    }

    /**
     * 根据主键获取使用频率详情
     * @param frequencyCode 主键
     * @return 实体
     */
    public DictFrequency getFrequencyByCode(String frequencyCode) {
        if (StringUtil.isBlank(frequencyCode)) {
            return null;
        }
        DictFrequency dictFrequency = dictFrequencyMapper.selectByPrimaryKey(frequencyCode);
        if (dictFrequency == null || YesOrNoEnum.YES.getValue().equals(dictFrequency.getIsDelete())) {
            return null;
        }
        return dictFrequency;
    }

    /**
     * 修改使用频率常量
     * @param dictFrequency 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateFrequency(DictFrequency dictFrequency) {
        validateFrequency(dictFrequency);
        DictFrequency dbDictFrequency = getFrequencyByCode(dictFrequency.getFrequencyCode());
        if (dbDictFrequency == null) {
            throw new BusinessFailException("使用频率不存在");
        }
        dictFrequency.setUpdateTime(new Date());
        dictFrequencyMapper.updateByPrimaryKeySelective(dictFrequency);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.FREQUENCY));
    }

    /**
     * 删除使用频率常量（逻辑删除）
     * @param dictFrequency 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteFrequency(DictFrequency dictFrequency) {
        DictFrequency dbDictFrequency = getFrequencyByCode(dictFrequency.getFrequencyCode());
        if (dbDictFrequency == null) {
            throw new BusinessFailException("使用频率不存在");
        }
        dictFrequencyMapper.deleteByPrimaryKey(dbDictFrequency.getFrequencyCode());
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.FREQUENCY));
    }

    /**
     * 启用使用频率
     * @param dictFrequency 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableFrequency(DictFrequency dictFrequency) {
        DictFrequency dbDictFrequency = getFrequencyByCode(dictFrequency.getFrequencyCode());
        if (dbDictFrequency == null) {
            throw new BusinessFailException("使用频率不存在");
        }
        if (YesOrNoEnum.YES.getValue().equals(dbDictFrequency.getIsEnabled())) {
            return;
        }
        dbDictFrequency.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbDictFrequency.setUpdateUserId(dictFrequency.getUpdateUserId());
        dbDictFrequency.setUpdateBy(dictFrequency.getUpdateBy());
        dbDictFrequency.setUpdateTime(new Date());
        dictFrequencyMapper.updateByPrimaryKeySelective(dbDictFrequency);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.FREQUENCY));
    }

    /**
     * 停用使用频率
     * @param dictFrequency 实体
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableFrequency(DictFrequency dictFrequency) {
        DictFrequency dbDictFrequency = getFrequencyByCode(dictFrequency.getFrequencyCode());
        if (dbDictFrequency == null) {
            throw new BusinessFailException("使用频率不存在");
        }
        if (YesOrNoEnum.NO.getValue().equals(dbDictFrequency.getIsEnabled())) {
            return;
        }
        dbDictFrequency.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbDictFrequency.setUpdateUserId(dictFrequency.getUpdateUserId());
        dbDictFrequency.setUpdateBy(dictFrequency.getUpdateBy());
        dbDictFrequency.setUpdateTime(new Date());
        dictFrequencyMapper.updateByPrimaryKeySelective(dbDictFrequency);
        redisUtil.del(String.format(RedisKeyPrefix.DICT_SYS_CODE, DictConstant.FREQUENCY));
    }

    /**
     * 获取系统全部的枚举
     * @return
     */
    public Map<String, Object> getAllEnums() {
        Map<String, Object> enumMap = new HashMap<>();
        enumMap.put("accountTypeEnum", AccountTypeEnum.allToMap());
        enumMap.put("acupointLocationTypeEnum", AcupointLocationTypeEnum.allToMap());
        enumMap.put("acupointSideTypeEnum", AcupointSideTypeEnum.allToMap());
        enumMap.put("acupointTypeEnum", AcupointTypeEnum.allToMap());
        enumMap.put("appointmentStatusEnum", AppointmentStatusEnum.allToMap());
        enumMap.put("auditStatusEnum", AuditStatusEnum.allToMap());
        enumMap.put("bodyPartEnum", BodyPartEnum.allToMap());
        enumMap.put("chargeOrderItemTypeEnum", ChargeOrderItemTypeEnum.allToMap());
        enumMap.put("checkStatusEnum", CheckStatusEnum.allToMap());
        enumMap.put("cycleEnum", CycleEnum.allToMap());
        enumMap.put("cycleUnitEnum", CycleUnitEnum.allToMap());
        enumMap.put("decotionMethodEnum", DecotionMethodEnum.allToMap());
        enumMap.put("deliveryStatusEnum", DeliveryStatusEnum.allToMap());
        enumMap.put("diagnosisFlagEnum", DiagnosisFlagEnum.allToMap());
        enumMap.put("diagnosisOpinionEnum", DiagnosisOpinionEnum.allToMap());
        enumMap.put("dispenseStatusEnum", DispenseStatusEnum.allToMap());
        enumMap.put("doctorTitleEnum", DoctorTitleEnum.allToMap());
        enumMap.put("dosageFormEnum", DosageFormEnum.allToMap());
        enumMap.put("drugCategoryEnum", DrugCategoryEnum.allToMap());
        enumMap.put("environmentEnum", EnvironmentEnum.allToMap());
        enumMap.put("ethnicityEnum", EthnicityEnum.allToMap());
        enumMap.put("examAuditStatusEnum", ExamAuditStatusEnum.allToMap());
        enumMap.put("examStatusEnum", ExamStatusEnum.allToMap());
        enumMap.put("examTypeEnum", ExamTypeEnum.allToMap());
        enumMap.put("execStatusEnum", ExecStatusEnum.allToMap());
        enumMap.put("fieldScopeEnum", FieldScopeEnum.allToMap());
        enumMap.put("fieldStyleEnum", FieldStyleEnum.allToMap());
        enumMap.put("fieldTypeEnum", FieldTypeEnum.allToMap());
        enumMap.put("fileSuffixTypeEnum", FileSuffixTypeEnum.allToMap());
        enumMap.put("fileTypeEnum", FileTypeEnum.allToMap());
        enumMap.put("followUpChannelEnum", FollowUpChannelEnum.allToMap());
        enumMap.put("followUpStatusEnum", FollowUpStatusEnum.allToMap());
        enumMap.put("genderEnum", GenderEnum.allToMap());
        enumMap.put("infusionIvDripRateEnum", InfusionIvDripRateEnum.allToMap());
        enumMap.put("labSampleTypeEnum", LabSampleTypeEnum.allToMap());
        enumMap.put("levelCodeEnum", LevelCodeEnum.allToMap());
        enumMap.put("medicalFieldLevelEnum", MedicalFieldLevelEnum.allToMap());
        enumMap.put("memberOrderStatusEnum", MemberOrderStatusEnum.allToMap());
        enumMap.put("memberTransTypeEnum", MemberTransTypeEnum.allToMap());
        enumMap.put("menuTypeEnum", MenuTypeEnum.allToMap());
        enumMap.put("operateTypeEnum", OperateTypeEnum.allToMap());
        enumMap.put("outboundTypeEnum", OutboundTypeEnum.allToMap());
        enumMap.put("packagingUnitEnum", PackagingUnitEnum.allToMap());
        enumMap.put("payChannelEnum", PayChannelEnum.allToMap());
        enumMap.put("payStatusEnum", PayStatusEnum.allToMap());
        enumMap.put("printCategoryEnum", PrintCategoryEnum.allToMap());
        enumMap.put("printTypeEnum", PrintTypeEnum.allToMap());
        enumMap.put("projectCategoryEnum", ProjectCategoryEnum.allToMap());
        enumMap.put("projectChargeTypeEnum", ProjectChargeTypeEnum.allToMap());
        enumMap.put("purchaseOrderStatusEnum", PurchaseOrderStatusEnum.allToMap());
        enumMap.put("queueStatusEnum", QueueStatusEnum.allToMap());
        enumMap.put("regChannelEnum", RegChannelEnum.allToMap());
        enumMap.put("regMethodEnum", RegMethodEnum.allToMap());
        enumMap.put("regStatusEnum", RegStatusEnum.allToMap());
        enumMap.put("regTypeEnum", RegTypeEnum.allToMap());
        enumMap.put("reqOrderStatusEnum", ReqOrderStatusEnum.allToMap());
        enumMap.put("reqSourceEnum", ReqSourceEnum.allToMap());
        enumMap.put("resultEnum", ResultEnum.allToMap());
        enumMap.put("rxTypeEnum", RxTypeEnum.allToMap());
        enumMap.put("settleStatusEnum", SettleStatusEnum.allToMap());
        enumMap.put("sexEnum", SexEnum.allToMap());
        enumMap.put("skinTestEnum", SkinTestEnum.allToMap());
        enumMap.put("stockLogTypeEnum", StockLogTypeEnum.allToMap());
        enumMap.put("stockOrderTypeEnum", StockOrderTypeEnum.allToMap());
        enumMap.put("tcmDailyDosageEnum", TcmDailyDosageEnum.allToMap());
        enumMap.put("tcmSingleDosageEnum", TcmSingleDosageEnum.allToMap());
        enumMap.put("templateCategoryEnum", TemplateCategoryEnum.allToMap());
        enumMap.put("templateTypeEnum", TemplateTypeEnum.allToMap());
        enumMap.put("unitTypeEnum", UnitTypeEnum.allToMap());
        enumMap.put("visitPeriodEnum", VisitPeriodEnum.allToMap());
        enumMap.put("visitStatusEnum", VisitStatusEnum.allToMap());
        enumMap.put("visitTypeEnum", VisitTypeEnum.allToMap());
        enumMap.put("yesOrNoEnum", YesOrNoEnum.allToMap());
        enumMap.put("drugChangeTypeEnum", DrugChangeTypeEnum.allToMap());
        enumMap.put("drugTransTypeEnum", DrugTransTypeEnum.allToMap());
        return enumMap;
    }

}
